package com.cloudcc.mobile.view.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudcc.cloudframe.CApplication;
import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.IEventLife;
import com.cloudcc.cloudframe.bus.MenuToggleEventR;
import com.cloudcc.cloudframe.bus.MessageNumX;
import com.cloudcc.cloudframe.net.ErrorInfo;
import com.cloudcc.cloudframe.net.ResultCallBack;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrClassicFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrDefaultHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrHandler;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.dao.impl.ApprovalEngineImpl;
import com.cloudcc.mobile.dialog.CallLogLoadingDialog;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.util.ViewUtils;
import com.cloudcc.mobile.view.base.BaseSlidingRightActivity;
import com.cloudcc.mobile.view.mymodel.ParseJson;
import com.cloudcc.mobile.view.mymodel.ParseJson.Zu;
import com.cloudcc.mobile.view.mymodel.moredata;
import com.cloudcc.mobile.view.mymodel.my;
import com.cloudcc.mobile.view.mymodel.mydaishenpi;
import com.cloudcc.mobile.view.mymodel.mypage;
import com.cloudcc.mobile.weight.EmptyLayout;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.litesuits.android.log.Log;
import com.mypage.utils.AnimViewUtils;
import com.mypage.view.activity.ApprovalPendingActivity;
import com.mypage.view.activity.ApproveActivityDialog;
import com.mypage.view.activity.MyApproval;
import com.mypage.view.activity.RefuseActivityDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;


/**
 * 审批(待办任务)
 * @author zys
 * @date 2015/12/08/12:00
 */public class TaskManagementActivty extends BaseSlidingRightActivity implements
		OnClickListener, ExpandableListView.OnChildClickListener,
		ExpandableListView.OnGroupClickListener ,IEventLife{
	// private TaskManagementAdapter adapter;
	// private ArrayList<TaskManagementModel> tasklist;
	private ApprovalEngineImpl engine = new ApprovalEngineImpl();
	public static TaskManagementActivty instance = null;
	private String userId;
	private EmptyLayout empty_loading;
	int i;
	@Bind(R.id.bar_back)
	ImageView back;
	@Bind(R.id.bar_title_text)
	TextView title;
	@Bind(R.id.bar_button_plus)
	ImageView imgplus;
	// @Bind(R.id.task_management_list)
	// ListView listview;
	@Bind(R.id.management_list_loadmore)
	PtrClassicFrameLayout mptrframe;
	int imageid;
	//toast
    @Bind(R.id.layoutTop)
    FrameLayout layoutTop;
    @Bind(R.id.topLayoutBackg)
     RelativeLayout topLayoutBackg;
    @Bind(R.id.toastHintContent)
     TextView toastHintContent;
    @Bind(R.id.imgDeletes)
     ImageView imgDeletes;
	@Bind(R.id.message_num_tz)
	TextView message_num_tz;
	@Bind(R.id.message_num_99)
	TextView message_num_99;

	private LinearLayout btnRatify;// 批准
	private LinearLayout btnRefuse;// 拒绝
	private LinearLayout btnAllocation;// 重新分配
	private ImageView btnRatifyimage,btnRefuseiamge,btnAllocationimage;
//	private LinearLayout layout;
	private FrameLayout frameLayouts;
	private boolean sign = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		Log.d("----","11111");
		instance = this;
		register();
		initBoardCase();
		expandableListView = (ExpandableListView) findViewById(R.id.expandablelist);
		frameLayouts = (FrameLayout) findViewById(R.id.frameLayouts);
		back.setOnClickListener(this);
		btnRatify = (LinearLayout) findViewById(R.id.btnRatify);
		btnRefuse = (LinearLayout) findViewById(R.id.btnRefuse);
		btnAllocation = (LinearLayout) findViewById(R.id.btnAllocation);
		btnRatifyimage = (ImageView) findViewById(R.id.pz_iv);
		btnRefuseiamge= (ImageView) findViewById(R.id.jujue_iv);
		btnAllocationimage= (ImageView) findViewById(R.id.cx_iv);
		callLogLoadingDialog  = new CallLogLoadingDialog(this,R.style.DialogLoadingTheme);
		btnRatify.setOnClickListener(this);
		btnRefuse.setOnClickListener(this);
		btnAllocation.setOnClickListener(this);
		if("taskfinish".equals(getIntent().getStringExtra("taskfinish"))){
			imgDeletes.setImageResource(0);
			topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
			toastHintContent.setText(R.string.piliangchuli);
			topLayoutBackg.setVisibility(View.GONE);
			layoutTop.setVisibility(View.VISIBLE);
			new AnimViewUtils().appearToast(topLayoutBackg);
		}else if("jujue".equals(getIntent().getStringExtra("zhuangtai"))){
			imgDeletes.setImageResource(0);
			topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
			toastHintContent.setText(R.string.piliangchuli);
			topLayoutBackg.setVisibility(View.GONE);
			layoutTop.setVisibility(View.VISIBLE);
			new AnimViewUtils().appearToast(topLayoutBackg);
		}
		//重新分配之前的处理提示  现在已经不用
		else if("chongxinshibai".equals(getIntent().getStringExtra("chongxin"))){
            imgDeletes.setImageResource(0);
            topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_red));
            toastHintContent.setText(R.string.shenpishibai);
            topLayoutBackg.setVisibility(View.GONE);
            layoutTop.setVisibility(View.VISIBLE);
            new AnimViewUtils().appearToast(topLayoutBackg);
        }else if("xiangqingpizhun".equals(getIntent().getStringExtra("taskfinish"))){
			imgDeletes.setImageResource(0);
			topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
			toastHintContent.setText(R.string.shenpichengg);
			topLayoutBackg.setVisibility(View.GONE);
			layoutTop.setVisibility(View.VISIBLE);
			new AnimViewUtils().appearToast(topLayoutBackg);
		}
//        else if("chongxinshibai".equals(getIntent().getStringExtra("chongxin"))){
//            imgDeletes.setImageResource(0);
//            topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
//            toastHintContent.setText(R.string.fenpeishiabi);
//            topLayoutBackg.setVisibility(View.GONE);
//            layoutTop.setVisibility(View.VISIBLE);
//            new AnimViewUtils().appearToast(topLayoutBackg);
//        }
		initEmpty();
		userId = UserManager.getManager().getUser().userId;
		title.setText("待审批");
		android.util.Log.d("-----","1111");
		initframe();
		initlistener();
		// 区分是否同时点击一个
		// expandableListView.setOnGroupExpandListener(new
		// ExpandableListView.OnGroupExpandListener() {
		// @Override
		// public void onGroupExpand(int groupPosition) {
		// int count =
		// expandableListView.getExpandableListAdapter().getGroupCount();
		// for(int j = 0; j < count; j++){
		// if(j != groupPosition){
		// expandableListView.collapseGroup(j);
		// }
		// }
		// }
		// });

	}


	@Override
	protected void onResume() {
		super.onResume();
		if(isno){
			btnRatifyimage.setImageDrawable(getResources().getDrawable(R.drawable.appro_pizhun_no));
			btnRefuseiamge.setImageDrawable(getResources().getDrawable(R.drawable.appro_jujue_no));
			btnAllocationimage.setImageDrawable(getResources().getDrawable(R.drawable.appro_chongxin_no));
		}else {
			btnRatifyimage.setImageDrawable(getResources().getDrawable(R.drawable.appro_pizhun_default));
			btnRefuseiamge.setImageDrawable(getResources().getDrawable(R.drawable.appro_jujue_default));
			btnAllocationimage.setImageDrawable(getResources().getDrawable(R.drawable.appro_chongxin_default));
		}
		MessageSetNCL();
	}

	private void initlistener() {
        imgplus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                EventEngine.post(new MenuToggleEventR(false, false));
            }
        });
		// 批准
		btnRatify.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				// .show();
				btnRatifyimage.setImageDrawable(getResources().getDrawable(R.drawable.appro_pizhun_press));
//				StringBuffer sb = new StringBuffer();
				StringBuffer sbisbeizhu= new StringBuffer();
				String sbisbeizhus;
				for (Map.Entry<String, Boolean> entry : checkmap.entrySet()) {

					if (true == entry.getValue()) {
						String workid = checknummap.get(entry.getKey());
						sb.append(workid);
						sb.append(";");
						sbisbeizhu.append(workid);
						sbisbeizhu.append(",");
					}
				}
				if (sb.toString() == null || "".equals(sb.toString())) {
					Toast.makeText(mContext, R.string.choseone, Toast.LENGTH_SHORT).show();
					return;

				}
				if(0<sb.length()&&sb.length()<30){
					sb = sb.delete(sb.length()-1,sb.length());
				}
                if(sbisbeizhu.length()<30){
					sbisbeizhu = sbisbeizhu.delete(sbisbeizhu.length()-1,sbisbeizhu.length());
                }

				Log.d("bianlimap", sb);
				//requestBeizhu(String.valueOf(sbisbeizhu),1,"Approved");
				sbisbeizhu = sbisbeizhu.delete(0,sbisbeizhu.length());

				Intent intent = null;

				intent = new Intent(TaskManagementActivty.this,
							ApproveActivityDialog.class);
					intent.putExtra("weizhi","liebiao");
				intent.putExtra("isRequired", "true");//
				intent.putExtra("workItemId", sb.toString());//
				startActivity(intent);

			}
		});
//		btnRefuse;// 拒绝
		btnRefuse.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				btnRefuseiamge.setImageDrawable(getResources().getDrawable(R.drawable.appro_jujue_press));
//				StringBuffer sb1 = new StringBuffer();
				StringBuffer sbisbeizhu1 = new StringBuffer();
				for (Map.Entry<String, Boolean> entry : checkmap.entrySet()) {

					if (true == entry.getValue()) {
						String workid = checknummap.get(entry.getKey());
						sb.append(workid);
						sb.append(";");
						sbisbeizhu1.append(workid);
						sbisbeizhu1.append(",");
					}
				}
				if (sb.toString() == null || "".equals(sb.toString())) {
					Toast.makeText(mContext, R.string.choseone, Toast.LENGTH_SHORT).show();
					return;

				}
				if(sb.length()<30){
					sb = sb.delete(sb.length()-1,sb.length());
				}
				if(sbisbeizhu1.length()<30){
					sbisbeizhu1 = sbisbeizhu1.delete(sbisbeizhu1.length()-1,sbisbeizhu1.length());
				}

				//requestBeizhu(String.valueOf(sbisbeizhu1),2,"Rejected");
				sbisbeizhu1 = sbisbeizhu1.delete(0,sbisbeizhu1.length());

			Intent	intent = new Intent(TaskManagementActivty.this,
						RefuseActivityDialog.class);
				intent.putExtra("weizhi","liebiao");
							intent.putExtra("isRequired", "true");//
							intent.putExtra("workItemId", sb.toString());//
			startActivity(intent);


			}
		});
		//重新分配
		btnAllocation.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				btnAllocationimage.setImageDrawable(getResources().getDrawable(R.drawable.appro_chongxin_press));
				// Toast.makeText(TaskManagementActivty.this,
				// "重新分配",Toast.LENGTH_SHORT).show();
//				StringBuffer sb2 = new StringBuffer();
				StringBuffer sbisbeizhu2 = new StringBuffer();
				for (Map.Entry<String, Boolean> entry : checkmap.entrySet()) {

					if (true == entry.getValue()) {
						String workid = checknummap.get(entry.getKey());
						sb.append(workid);
						sb.append(";");
						sbisbeizhu2.append(workid);
						sbisbeizhu2.append(",");
					}
				}
				if (sb.toString() == null || "".equals(sb.toString())) {
					Toast.makeText(mContext, R.string.choseone, Toast.LENGTH_SHORT).show();
					return;
				}
				if(sb.length()<30){
					sb = sb.delete(sb.length()-1,sb.length());
				}

//				requestBeizhu(String.valueOf(sbisbeizhu2),3);
					Intent intents = new Intent(TaskManagementActivty.this,
							MyApproval.class);
					intents.putExtra("weizhi","liebiao");
					intents.putExtra("personname", map.get(groupList.get(0))
							.get(0));//
				intents.putExtra("workItemId", sb.toString());//
				startActivity(intents);
				}
		});
	}

	public void initBoardCase() {
		IntentFilter filter = new IntentFilter();
		filter.addAction("taskfinish");
		registerReceiver(broadcastReceiver, filter);
	}

	public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			unregisterReceiver(this);
			TaskManagementActivty.this.finish();
		}
	};
	// 存储内容
	Map<String, List<my>> map = new HashMap<String, List<my>>();
	// 存储页数
	Map<String, mypage> page = new HashMap<String, mypage>();
	//存储alldata
	Map<String, String> pagenum = new HashMap<String, String>();
	// 存储类型
	Map<String, String> fr = new HashMap<String, String>();
	// 存储摘要
	Map<String, String> zhaiyaomap = new HashMap<String, String>();
	// 选中的跟id联系起来取到workid
	Map<String, String> checknummap = new HashMap<String, String>();
	// 存储按钮
	Map<String, Boolean> anniumap = new HashMap<String, Boolean>();
	ArrayList<my> childTemp;
	Context context;
	// 存储点击状态
	// private Map<String, Integer> checkimageid = new HashMap<String,
	// Integer>();
	// private Map<Integer, Integer> checkimageid1 = new HashMap<Integer,
	// Integer>();
	private Map<String, Boolean> checkmap = new HashMap<String, Boolean>();
	Button loadmore;
	LinearLayout loadmoerll;
	private void initEmpty() {
		empty_loading = new EmptyLayout(mContext);
		empty_loading.setLayoutParams(new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		ViewUtils.addListViewEmptyView(mContext, expandableListView,
				empty_loading);
		empty_loading.empty();

	}

	public void initframe() {
		mptrframe.setLastUpdateTimeRelateObject(this);
		mptrframe.setPtrHandler(new PtrHandler() {
			@Override
			public boolean checkCanDoRefresh(PtrFrameLayout frame,
					View content, View header) {
				return PtrDefaultHandler.checkContentCanBePulledDown(frame,
						expandableListView, header);
			}

			@Override
			public void onRefreshBegin(PtrFrameLayout frame) {
				sign = false;
				initdata();
			}
		});
		mptrframe.setLoadingMinTime(1000);
		mptrframe.setDurationToClose(200);
		mptrframe.setDurationToCloseHeader(1000);
		mptrframe.setResistance(1.7f);
		mptrframe.setRatioOfHeaderHeightToRefresh(1.2f);
		mptrframe.setPullToRefresh(false);
		mptrframe.setKeepHeaderWhenRefresh(true);
		mptrframe.postDelayed(new Runnable() {
			@Override
			public void run() {
				mptrframe.autoRefresh();
			}
		}, 150);
	}
	List<my> data = new ArrayList<my>();
	// 判断加载更多按钮位置
	Map<Integer, Integer> jiazaiweizhimap = new HashMap<Integer, Integer>();
//	int num = 1;
	private boolean isno = false;
	public void initdata() {
		engine.GetManagementTask_z(userId,CApplication.approNum, new ResultCallBack<JsonObject>() {
			@Override
			public void onSuccess(JsonObject t) {
				JsonObject zhaoyao = (JsonObject) t.get("zymap");
				// JsonElement jsonElement2 = t.get("zymap");
				Log.d("getfield", "解析下一步");
				Gson gson = new Gson();
				ParseJson json = gson.fromJson(t, ParseJson.class);
				if (json.resultList != null && json.resultList.size() > 0) {
					isno = false;
					frameLayouts.setVisibility(View.GONE);
                    btnRatifyimage.setImageDrawable(getResources().getDrawable(R.drawable.appro_pizhun_default));
                    btnRefuseiamge.setImageDrawable(getResources().getDrawable(R.drawable.appro_jujue_default));
                    btnAllocationimage.setImageDrawable(getResources().getDrawable(R.drawable.appro_chongxin_default));
					btnRatify.setEnabled(true);
					btnRefuse.setEnabled(true);
					btnAllocation.setEnabled(true);
//					layout.setVisibility(View.VISIBLE);
				} else {
					isno = true;
					frameLayouts.setVisibility(View.VISIBLE);
					btnRatifyimage.setImageDrawable(null);
					btnRefuseiamge.setImageDrawable(null);
					btnAllocationimage.setImageDrawable(null);
                    btnRatifyimage.setImageDrawable(getResources().getDrawable(R.drawable.appro_pizhun_no));
                    btnRefuseiamge.setImageDrawable(getResources().getDrawable(R.drawable.appro_jujue_no));
                    btnAllocationimage.setImageDrawable(getResources().getDrawable(R.drawable.appro_chongxin_no));
					btnRatify.setEnabled(false);
					btnRefuse.setEnabled(false);
					btnAllocation.setEnabled(false);


				}
                groupList = new ArrayList<String>();
				// com.litesuits.android.log.Log.d("taskmodel",
				// json.resultList.get(0).fr);
				List<Zu> list = new ArrayList<Zu>();
				list = json.resultList;
				// groupList = new ArrayList<String>();
				for (int i = 0; i < list.size(); i++) {

					// data里面是一个个的待审批数据model
					data = list.get(i).data;
					if ((data != null)) {
						String fenzu = data.get(0).objtype;
						groupList.add(fenzu);
					}
					int lenth = data.size();
					// zonglen = lenth+zonglen;
					jiazaiweizhimap.put(i, lenth);
					Log.d("weizhi", i + "::" + lenth);
					// 去重
					for (int l = 0; l < groupList.size(); l++) // 外循环是循环的次数
					{
						for (int j = groupList.size() - 1; j > l; j--) // 内循环是
																		// 外循环一次比较的次数
						{

							if (groupList.get(j).equals(groupList.get(l))) {
								groupList.remove(j);
							}

						}
					}
					Log.d("quchongyihoude", groupList.size() + groupList.get(i));
					childTemp = new ArrayList<my>();
					mypage page1 = new mypage();
					page1 = list.get(i).page;
					map.put(groupList.get(i), data);
					page.put(groupList.get(i), page1);
					pagenum.put(groupList.get(i), page1.alldata);
					for (int j = 0; j < data.size(); j++) {
						String key = data.get(j).objid;
						if (!("".equals(zhaoyao.get(key)))
								&& null != zhaoyao.get(key)) {
							String zhaiyao = zhaoyao.get(key).getAsString();
							// Log.d("zhaiyao", "zhaiyao:" + zhaiyao);
							zhaiyaomap.put(key, zhaiyao);
						}

					}

					// 分组标识
					String frnzu = list.get(i).fr;
					fr.put(groupList.get(i), frnzu);
					Log.d("qufen", "数据:" + groupList.get(i) + "::" + frnzu);
					// for (int q = 0; q < data.size(); q++) {
					// Log.d("shujuduibudui", "数据:" + page1.allpage + "::"
					// + data.get(q).objtype);
					// }
				}
				// 设置默认的imageid防止集合报错
				// for (int h = 0; h < groupList.size(); h++) {
				// checkimageid.put(groupList.get(h), 0);
				// checkimageid1.put(h, 0);
				// }
				// 设置默认的checkmap跟anniumap
				checkmap = new HashMap<String, Boolean>();
				for (int l = 0; l < groupList.size(); l++) {
					for (int i = 0; i < map.get(groupList.get(l)).size(); i++) {
						checkmap.put(map.get(groupList.get(l)).get(i).objid,
								false);
						anniumap.put(map.get(groupList.get(l)).get(i).objid,
								false);
					}
				}

				// InitData1();
				adapter = new MyexpandableListAdapter(
						TaskManagementActivty.this);
				expandableListView.setAdapter(adapter);
				mptrframe.refreshComplete();
				sign = true;
				// 展开
//				for (int i = 0; i < adapter.getGroupCount(); i++) {

				try {
					expandableListView.expandGroup(0);
				} catch (Exception e) {
					e.printStackTrace();
				}
//				}
				expandableListView
						.setOnChildClickListener(TaskManagementActivty.this);
				expandableListView
						.setOnGroupClickListener(TaskManagementActivty.this);

			}
			@Override
			public void onFailure(ErrorInfo error) {
				sign = true;
				mptrframe.refreshComplete();
				Toast.makeText(TaskManagementActivty.this, R.string.qingqiushibai,
						Toast.LENGTH_SHORT).show();
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unRegister();
		
	}

	@Override
	public int getLayoutId() {
		return R.layout.taskmanagement;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bar_back:
			TaskManagementActivty.this.finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		// adapter.clear();
		mptrframe.autoRefresh();
	}

	private ExpandableListView expandableListView;
	private ArrayList<String> groupList = new ArrayList<String>();

	private MyexpandableListAdapter adapter;
	// 原来的刷新选中个数+删除sb
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			// int i = (int) msg.obj;
			// button.getTag();
			// tvmap.get(i).setText(getChecked(i).toString());
			sb.delete(0, sb.length());
			// adapter.notifyDataSetChanged();
		};
	};

	StringBuffer sb = new StringBuffer();;

	@Override
	public void register() {
		EventEngine.register(this);
	}

	@Override
	public void unRegister() {
		EventEngine.uregister(this);
	}

	/***
	 * 数据源
	 * 
	 * @author Administrator
	 * 
	 */
	class MyexpandableListAdapter extends BaseExpandableListAdapter {
		private Context context;
		private LayoutInflater inflater;

		// private Button loadMore;

		public MyexpandableListAdapter(Context context) {
			this.context = context;
			inflater = LayoutInflater.from(context);

		}

		// 返回父列表个数
		@Override
		public int getGroupCount() {

			return groupList.size();
		}

		// 返回子列表个数
		@Override
		public int getChildrenCount(int groupPosition) {

			return map.get(groupList.get(groupPosition)).size();
		}

		@Override
		public Object getGroup(int groupPosition) {

			return groupList.get(groupPosition);
		}

		// public Object getChecked(int groupPosition) {
		//
		// return checkimageid1.get(groupPosition);
		// }

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return map.get(groupList.get(groupPosition)).get(childPosition);
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public boolean hasStableIds() {

			return true;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			GroupHolder groupHolder = null;
			if (convertView == null) {
				groupHolder = new GroupHolder();
				convertView = inflater.inflate(R.layout.group, null);
				groupHolder.buttonTextView = (TextView) convertView
						.findViewById(R.id.tvcheck);
				groupHolder.buttonTextView.setTag(groupPosition);
				groupHolder.textView = (TextView) convertView
						.findViewById(R.id.group);// diyi
				groupHolder.imageView = (ImageView) convertView
						.findViewById(R.id.image);
				//文字加粗

//				TextPaint tp = groupHolder.textView.getPaint();
//				tp.setFakeBoldText(true);
				convertView.setTag(groupHolder);

			} else {
				groupHolder = (GroupHolder) convertView.getTag();
			}
//			int a = getChildrenCount(groupPosition);
//			try {
//				int a = (Integer.parseInt(page.get(groupList.get(groupPosition)).alldata.trim()));
//				Log.d("xianmshichandu", "长度是：" + a);
				groupHolder.buttonTextView.setText(pagenum.get(groupList.get(groupPosition)).trim());
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
			groupHolder.textView.setText(getGroup(groupPosition).toString());

			if (isExpanded) {
				groupHolder.imageView.setImageResource(R.drawable.angleup);
			} else {
				groupHolder.imageView.setImageResource(R.drawable.anglerdown);
			}
			return convertView;
		}

		@Override
		public View getChildView(final int groupPosition,
				final int childPosition, boolean isLastChild, View convertView,
				ViewGroup parent) {
			childHolder holder;
			// if (convertView == null) {
			holder = new childHolder();
			convertView = inflater.inflate(R.layout.item, null);
			loadmore = (Button) convertView.findViewById(R.id.loader);
			loadmoerll = (LinearLayout) convertView.findViewById(R.id.loadMore);
			holder.textView = (TextView) convertView
					.findViewById(R.id.zhaiyaotext);
			holder.textView1 = (TextView) convertView
					.findViewById(R.id.laizi);
			holder.textView2 = (TextView) convertView
					.findViewById(R.id.shijian);
			holder.textview3 = (TextView) convertView
					.findViewById(R.id.mingcheng);
			holder.box = (CheckBox) convertView.findViewById(R.id.shenpi_check);

			convertView.setTag(holder);
			// } else {
			// holder = (childHolder) convertView.getTag();
			// }

			Log.d("anniudedianji",
					map.get(groupList.get(groupPosition)).size()
							+ ":allpage:"
							+ (Integer.parseInt(page.get(groupList
									.get(groupPosition)).allpage))
							+ ":sv_page:"
							+ (Integer.parseInt(page.get(groupList
									.get(groupPosition)).sv_page))
							+ ":childPosition:" + childPosition
							+ ":groupPosition:" + groupPosition);
			// if ((map.get(groupList.get(groupPosition)).size() ==
			// (childPosition + 1))) {

			if ((Integer
					.parseInt(page.get(groupList.get(groupPosition)).allpage)) > (Integer
					.parseInt(page.get(groupList.get(groupPosition)).sv_page))) {
				anniumap.put(
						map.get(groupList.get(groupPosition))
								.get(childPosition).objid, true);

			} else {
				anniumap.put(
						map.get(groupList.get(groupPosition))
								.get(childPosition).objid, false);

			}
			// }
			// Log.d("anniushezhi",
			// "::::"+anniumap.get(map.get(groupList.get(groupPosition)).get(
			// childPosition).objid));
			// if(anniumap.get(map.get(groupList.get(groupPosition)).get(
			// childPosition).objid)){
			Log.d("tttttttttt", groupPosition + "::" + childPosition + "::"
					+ (jiazaiweizhimap.get(groupPosition) == childPosition + 1)
					+ ":按钮位置:" + jiazaiweizhimap.get(groupPosition));

			if (anniumap.get(map.get(groupList.get(groupPosition)).get(
					childPosition).objid)) {
				if (jiazaiweizhimap.get(groupPosition) == childPosition + 1) {
					loadmoerll.setVisibility(View.VISIBLE);
					loadmore.setText("加载更多");
					loadmore.setEnabled(true);
					loadmore.setTag((groupPosition));
					Log.d("shezhitag", groupPosition);
				} else {
					loadmoerll.setVisibility(View.GONE);
					// loadMore.setText("已经到底了");
					loadmore.setEnabled(false);
				}
			}

			//
			holder.textView.setTextSize(13);
			holder.textview3.setText(map.get(groupList.get(groupPosition)).get(
					childPosition).objname);
			//来自换成ownername
			holder.textView1.setText("来自"+map.get(groupList.get(groupPosition)).get(
					childPosition).ownername);

			holder.textView2.setText(map.get(groupList.get(groupPosition)).get(
					childPosition).actdate);
			String mStr = zhaiyaomap.get(map.get(groupList.get(groupPosition))
					.get(childPosition).objid);
			if (null != mStr && !("".equals(mStr))) {
				Log.d("maolenth", "sfc::" + map.size());
				holder.textView.setText(zhaiyaomap.get(
						map.get(groupList.get(groupPosition))
								.get(childPosition).objid).replace("&nbsp;",
						" "));
			}

			Log.d("zuihoude", "haoma" + groupPosition + "   " + childPosition);
			loadmore.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {

					loadmore.setText("正在加载数据....");
					loadmore.setEnabled(false);
					final int index = (Integer) arg0.getTag();
					Log.d("shezhitag", index);
					int sv_page;
					if(CApplication.approNum==1){
						sv_page = Integer.parseInt((page.get(groupList
								.get(index)).sv_page)) + 1;
					}else{
						sv_page = CApplication.approNum+1;
					}

					String prefix = fr.get(groupList.get(index));
					Log.d("prefix", "区分阿牛" + prefix + "::" + index);
					CApplication.approNum = sv_page;
//					num = sv_page;
					engine.GetManagementTask_jzgd(sv_page, prefix,
							new ResultCallBack<JsonObject>() {
								@Override
								public void onSuccess(JsonObject t) {

									Gson gson = new Gson();
									moredata moredata = gson.fromJson(t,
											moredata.class);
									Log.d("moredata",
											moredata.jsonObject.allpage);
									List<my> list = new ArrayList<my>();
									list = moredata.jsonList;
									if (list.size() > 0) {
										for (int i = 0; i < list.size(); i++) {
											Log.d("moredata",
													list.get(i).objtype);
											map.get((list.get(i).objtype)).add(
													list.get(i));
											zhaiyaomap.put(list.get(i).objid,
													list.get(i).zhaiyao);
											checkmap.put(list.get(i).objid,
													false);
											anniumap.put(list.get(i).objid,
													false);
										}
										page.put((list.get(i).objtype),
												moredata.jsonObject);
										int lenth = list.size();
										int lenth1 = jiazaiweizhimap.get(index);
										jiazaiweizhimap.put(index, lenth1
												+ lenth);
										// Toast.makeText(TaskManagementActivty.this,
										// "获取任务成功", Toast.LENGTH_SHORT).show();
										// }
									}
									adapter.notifyDataSetChanged();
									// adapter.loadMore.setText("点击加载数据");
								}

								@Override
								public void onFailure(ErrorInfo error) {
									sign = true;
									mptrframe.refreshComplete();
									Toast.makeText(TaskManagementActivty.this,
											R.string.qingqiushibai, Toast.LENGTH_SHORT)
											.show();
								}
							});

				}
			});
			holder.box.setTag(map.get(groupList.get(groupPosition)).get(
					childPosition).objid);
			checknummap
					.put(map.get(groupList.get(groupPosition)).get(
							childPosition).objid,
							map.get(groupList.get(groupPosition)).get(
									childPosition).workItemid);
			holder.box.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// Log.d("biahzi", "zhege "+groupPosition);
					// 先把View转型为CheckBox
					CheckBox c = (CheckBox) v;
					// 获取当前控件的Tag，即与其绑定的position
					String po = (String) c.getTag();
					// 判断当前控件是否选中
					if (c.isChecked()) {
						// 若选中，就根据绑定的position找到相对应的控件设置状态为turn
						checkmap.put(po, true);
					} else {
						// 若未选中，就根据绑定的position找到相对应的控件设置状态为false
						checkmap.put(po, false);
					}

					// if
					// (checkmap.get(map.get(groupList.get(groupPosition)).get(
					// childPosition).objid)) {
					//
					// int a = (checkimageid.get(groupList.get(groupPosition)));
					// Log.d("wozaichulishuju",
					// map.get(groupList.get(groupPosition)).get(
					// childPosition).objid
					// + "::"
					// + groupPosition
					// + "::"
					// + (checkimageid.get(groupList
					// .get(groupPosition))));
					// a = a + 1;
					// Log.d("wozaichulishuju", a);
					// checkimageid.put(groupList.get(groupPosition), a);
					// checkimageid1.put(groupPosition, a);
					// // button.setText(a+"");
					// } else if (!checkmap.get(map.get(
					// groupList.get(groupPosition)).get(childPosition).objid))
					// {
					// int a = (checkimageid.get(groupList.get(groupPosition)));
					// a--;
					// checkimageid.put(groupList.get(groupPosition), a);
					// Log.d("wozaichulishuju",
					// map.get(groupList.get(groupPosition)).get(
					// childPosition).objid
					// + "::"
					// + groupPosition
					// + "::"
					// + (checkimageid.get(groupList
					// .get(groupPosition))));
					// checkimageid1.put(groupPosition, a);
					//
					// }

					Message oneMsg = handler.obtainMessage();
					oneMsg.obj = groupPosition;
					oneMsg.what = 1;
					handler.sendMessage(oneMsg);

				}
			});

			// 根据map集合获取此时box的状态
			holder.box.setChecked(checkmap.get(map.get(
					groupList.get(groupPosition)).get(childPosition).objid));
			Log.d("shishashisha",
					checkmap.get(map.get(groupList.get(groupPosition)).get(
							childPosition).objid)
							+ "::" + jiancha.size());
			if (checkmap.get(map.get(groupList.get(groupPosition)).get(
					childPosition).objid)) {

				String workid = map.get(groupList.get(groupPosition)).get(
						childPosition).workItemid;
				if (jiancha.size() > 0) {
					for (int i = 0; i < jiancha.size(); i++) {
						if (!(jiancha.get(i).equals(workid))) {
							jiancha.add(workid);

						}
					}
				} else {
					jiancha.add(workid);
				}

			}
			// else{
			// String workid =
			// map.get(groupList.get(groupPosition)).get(childPosition).workItemid;
			// if((jiancha.get(i).equals(workid))){
			// jiancha.remove(i);
			//
			// }
			// }

			// for (int i = 0; i < jiancha.size(); i++) {
			// sb.append(jiancha.get(i));
			// sb.append(";");
			// }

			return convertView;
		}

		List<String> jiancha = new ArrayList<String>();

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return sign;
		}
	}

	// class myonclick implements OnClickListener {
	//
	// @Override
	// public void onClick(View arg0) {}
	//
	// }

	@Override
	public boolean onGroupClick(final ExpandableListView parent, final View v,
			int groupPosition, final long id) {

		return false;
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		Log.d("shibushia", "shuju::::" + sb.toString());
		// Toast.makeText(this, "点击child", 1).show();
		Intent intent = new Intent(TaskManagementActivty.this,
				ApprovalPendingActivity.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("extra", map.get(groupList.get(groupPosition))
				.get(childPosition));

		Log.d("bianlimao", "hahha" + zhaiyaomap.size());

		intent.putExtra(
				"zhaiyao",
				zhaiyaomap.get(map.get(groupList.get(groupPosition)).get(
						childPosition).objid));
		intent.putExtras(bundle);

		startActivity(intent);
		return true;
	}

	class GroupHolder {
		TextView textView;
		ImageView imageView;
		TextView buttonTextView;
	}

	class childHolder {
		TextView textView, textView1, textView2, textview3;
		CheckBox box;
		// Button loadMore;
	}
	private void MessageSetNCL(){
		long messagenum = 0;
		messagenum= RunTimeManager.getInstance().getMessageNx();
		message_num_tz.setText(messagenum+"");

		if(messagenum>0) {
			message_num_tz.setVisibility(View.VISIBLE);
			if(messagenum>99) {
				message_num_tz.setVisibility(View.INVISIBLE);
				message_num_99.setVisibility(View.VISIBLE);
			}

		}else {
			message_num_tz.setVisibility(View.INVISIBLE);
			message_num_99.setVisibility(View.INVISIBLE);
		}

	}

	//接收消息数
	public void onEventMainThread(MessageNumX event) {

		message_num_tz.setText(event.messageNumX+"");
		if(event.messageNumX>0) {
			message_num_tz.setVisibility(View.VISIBLE);
			if(event.messageNumX>99) {
				message_num_tz.setVisibility(View.INVISIBLE);
				message_num_99.setVisibility(View.VISIBLE);
			}

		}else {
			message_num_tz.setVisibility(View.INVISIBLE);
			message_num_99.setVisibility(View.INVISIBLE);
		}

	}

	public void requestBeizhu(final String ids, final int i, final String zghuangtai) {
		// 拒绝
		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("binding", RunTimeManager.getInstance()
				.getServerBinding());
		params.addBodyParameter("serviceName", "isRequired");
		params.addBodyParameter("ids", ids);
		utils.send(HttpRequest.HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						Intent intent = null;
						if(1==i){
							 intent = new Intent(TaskManagementActivty.this,
									ApproveActivityDialog.class);
						}else if(2==i){
							Intent intentb = new Intent(TaskManagementActivty.this,
									RefuseActivityDialog.class);
						}
							// 正式获取的时候把双引号去掉，现在只是加载的是静止的数据
						intent.putExtra("isRequired", "false");//
						intent.putExtra("workItemId", sb.toString());//
						// 正式获取的时候把双引号去掉，现在只是加载的是静止的数据
						startActivity(intent);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						String result = arg0.result;
						Gson gson = new Gson();
						mydaishenpi ss = gson.fromJson(arg0.result,
								mydaishenpi.class);
						if("true".equals(ss.data.isRequired)){

						Intent intent = null;
							if (1 == i) {
								intent = new Intent(TaskManagementActivty.this,
										ApproveActivityDialog.class);
								intent.putExtra("weizhi","liebiao");
							} else if (2 == i) {
								intent = new Intent(TaskManagementActivty.this,
										RefuseActivityDialog.class);
								intent.putExtra("weizhi","liebiao");
							}
							intent.putExtra("isRequired", ss.data.isRequired);//
							intent.putExtra("workItemId", sb.toString());//
							startActivity(intent);

						}else{
							zidong(zghuangtai);
						}
					}
				});

	}
	CallLogLoadingDialog callLogLoadingDialog ;
	public void zidong(String zhuangtai){
		pizhun(zhuangtai);
//		startProgressDialog();
		callLogLoadingDialog.show();
		callLogLoadingDialog.settext("处理中...");
	}
	public void pizhun(String zhuangtai) {
		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();

		params.addBodyParameter("userId",
				UserManager.getManager().getUser().userId);
		params.addBodyParameter("binding", RunTimeManager.getInstance()
				.getServerBinding());

		params.addBodyParameter("serviceName", "ajaxProcessMassThread");

		params.addBodyParameter("workItemId", sb.toString());
		params.addBodyParameter("actionType",zhuangtai);
		params.addBodyParameter("comments", "");
		params.addBodyParameter("fprId", "");
		Log.d("hahahha", UrlManager.getInterfaceUrl() + params);
		utils.send(HttpRequest.HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
				new RequestCallBack<String>() {
					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						callLogLoadingDialog.dismiss();
						imgDeletes.setImageResource(0);
						topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_red));
						toastHintContent.setText(R.string.shenpishibai);
						topLayoutBackg.setVisibility(View.GONE);
						layoutTop.setVisibility(View.VISIBLE);
						new AnimViewUtils().appearToast(topLayoutBackg);
						sb = sb.delete(0,sb.length());
					}
					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						String result = arg0.result;
						initframe();
						callLogLoadingDialog.dismiss();
						imgDeletes.setImageResource(0);
						topLayoutBackg.setBackground(getResources().getDrawable(R.drawable.toast_hint_beau));
						toastHintContent.setText(R.string.piliangchuli);
						topLayoutBackg.setVisibility(View.GONE);
						layoutTop.setVisibility(View.VISIBLE);
						new AnimViewUtils().appearToast(topLayoutBackg);
						sb = sb.delete(0,sb.length());
					}
				});

	}
}